package main

import "fmt"

// 链表包含一个数据块和一个指针

type L struct {
	Head *Node
}

type Node struct {
	Data     interface{}
	NextList *Node
}

func main() {
	list := L{}

	list.insert(10)
	list.insert(20)
	list.insert(30)
	list.insert(40)

	fmt.Println("Initial LinkedList:")
	list.display()

	list.delete(20)
	list.delete(30)

	fmt.Println("LinkedList after deletion:")
	list.display()
}

// 插入
func (li *L) insert(data interface{}) {
	node := &Node{
		Data:     data,
		NextList: nil,
	}
	if li.Head != nil {
		current := li.Head
		for current.NextList != nil {
			current = current.NextList
		}
		current.NextList = node
	} else {
		li.Head = node
	}

}

// 删除
func (li *L) delete(data interface{}) {
	if li.Head == nil {
		return
	}
	if li.Head.Data == data {
		li.Head = li.Head.NextList
	}
	current := li.Head
	for current.NextList != nil {
		if current.NextList.Data == data {
			current.NextList = current.NextList.NextList
			return
		}
	}
}

func (li *L) display() {
	if li.Head == nil {
		return
	}
	current := li.Head
	for current != nil {
		fmt.Println(current.Data)
		current = current.NextList
	}
}
